<script>
$( document ).ready(function() {
	$("#add-new-button").click(function() {
        var tag = "<div class=\"row\"><div class=\"large-3 medium-3 columns\"><label class=\"profile inline\"></label></div><div class=\"large-12 medium-12 small-14 columns margin-14px-bot\"><div class=\"row collapse\"><div class=\"small-14 columns\"><input type=\"email\" class=\"border-blue\" required name=\"addEmailSecondary\"/><small class=\"error\">An email address is required.</small></div><div class=\"small-2 columns\"><a><label class=\"border-blue postfix delete-email-add fa fa-trash-o postfix-icon\"></label></a></div></div></div></div><div class=\"row mark-here\"></div>";
        $(tag).insertAfter(".mark-here:last");
   });
	
	$(".delete-email-added").click(function () {
		var temp = $(this).parent().parent().siblings(".small-14.columns").children();
		if (temp.is("#primaryEmail")) {
			$.confirm({
				'message'	: "You can't delete primary email.",
				'buttons'	: {
					'OK'	: {
						'action': function(){
							$("#confirmBox").slideUp();
						}	// Nothing to do in this case. You can as well omit the action property.
					}
				}
			});
		}
		else {
			$(this).parent().parent().parent().parent().parent().remove();
		}
	});
	
	$(document).on('click', '.delete-email-add', function() {
		$(this).parent().parent().parent().parent().parent().remove();
	});
	
	$("[name='userEmail']").change(function () {
		$("#primaryEmail").attr("name", "alreadyAddedEmailSecondary");
		$("#primaryEmail").removeAttr("id");
		var temp = $(this).parent().siblings(".large-12.medium-12.small-14.columns").children().children().siblings(".small-14.columns").children();
		
		if (temp.get(0) == undefined) {
			$(this).parent().siblings(".large-12.medium-12.small-14.columns").children().children().attr('name', 'primaryEmail').attr('id', 'primaryEmail');
		}
		else {			
			$(this).parent().siblings(".large-12.medium-12.small-14.columns").children().children().siblings(".small-14.columns").children().attr('name', 'primaryEmail').attr('id', 'primaryEmail');
		}	    
	});
	
// 	// Show currency list
// 	var url_rates = 'https://bitpay.com/api/rates';
//     $.getJSON(url_rates, function (jsonRates) {
//         if (jsonRates === null) {
//             alert('fail :');
//         } else {
//             // sort currency list by name
//             jsonRates = jsonRates.sort(function (a, b) {
//                 if (a.code < b.code) return -1;
//                 if (b.code < a.code) return 1;
//                 return 0;
//             });
            
//             // show currency list
//             $.each(jsonRates, function (idx, obj) {
//             	$("#currency-list").append('<option value =" '+ obj.code + ' "> ' + obj.code + ' - ' + obj.name + '</option>') ;
//             });
//         }
//     });
});
</script>
